package com.example.myaccount.adapter;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.example.myaccount.activity.AnalysisActivity;
import com.example.myaccount.activity.MainActivity;
import com.example.myaccount.R;

public class LvSettingsAdapter extends BaseAdapter implements AdapterView.OnItemClickListener {

    private int[] imagesId={R.drawable.ic_home,R.drawable.ic_history,R.drawable.ic_analysis,R.drawable.ic_account,R.drawable.ic_personal};
    private int[] imagesCheckedId={R.drawable.ic_home_checked,R.drawable.ic_history_checked,R.drawable.ic_analysis_checked,R.drawable.ic_account_checked,R.drawable.ic_personal_checked};
    private	int[] names={R.string.settings_home,R.string.settings_history,R.string.settings_analysis,R.string.settings_account,R.string.settings_presonal};
    private int checkedPosition =0;
    private int colorBlue= ContextCompat.getColor(MainActivity.getContext(),R.color.text_color_blue);
    private int colorGrey = ContextCompat.getColor(MainActivity.getContext(),R.color.text_color_grey);

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(MainActivity.getContext());
        View root = inflater.inflate(R.layout.layout_list_option,null);

        TextView mTvName =root.findViewById(R.id.tv_option);
        ImageView mIvIcon = root.findViewById(R.id.iv_icon);

        mTvName.setText(names[position]);
        if(position==0){
            mIvIcon.setImageResource(imagesCheckedId[position]);
            mTvName.setTextColor(colorBlue);
        }else{
            mIvIcon.setImageResource(imagesId[position]);
        }
        return root;
    }
    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public View getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @SuppressLint("ResourceAsColor")
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        View checkedView = parent.getChildAt(checkedPosition);

        ImageView mIvIcon = view.findViewById(R.id.iv_icon);
        TextView mTvOption = view.findViewById(R.id.tv_option);
        ImageView mIvCheckedIcon = checkedView.findViewById(R.id.iv_icon);
        TextView mTvCheckedOption = checkedView.findViewById(R.id.tv_option);

        if(checkedPosition!=position){
            mIvCheckedIcon.setImageResource(imagesId[checkedPosition]);
            mTvCheckedOption.setTextColor(colorGrey);
            mIvIcon.setImageResource(imagesCheckedId[position]);
            mTvOption.setTextColor(colorBlue);
            checkedPosition =position;
        }

        if(checkedPosition==2){
            Intent intent = new Intent(MainActivity.getContext(), AnalysisActivity.class);
            MainActivity.getContext().startActivity(intent);
        }

    }
}
